package cn.edu.ctbu.api01.restcontroller;

import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/login")
public class LoginRestController {
    @Autowired
    StudentService studentService;
    @GetMapping("/user")
    public int getUser(HttpServletRequest httprequest,String username,String password) {
        //判断用户名和密码是否存在
        List<Student> student=studentService.findByNameAndPassword(username,password);
        if(student.size()>0) {
            //如果存在，写入session
            HttpSession session=httprequest.getSession();
            session.setAttribute("userid",student.get(0).getId());
            session.setAttribute("user",student.get(0));
            return 1; //成功
        }
        else{
            //不存在
            return -1; //出错
        }

    }
}
